<?php
class IndexController extends  Zendvn_Controller_Action{
	
	private $_arrParam;
	private $_mainAction;
	protected $_currentController;
	
	public function init(){
		
	 	
	    $this->_arrParam = $this->_request->getParams();	
		$this->loadTemplate(TEMPLATE_PATH . '/default/system');
		$this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam; 
	    
	    $ns = new Zend_Session_Namespace('lang');
       
    
	}
	
    public function indexAction(){
     
    	 if($this->_request->isPost()==true)
		{	
			$data           = $this->_arrParam;
			$user           = $data['user'];
			$pass           = md5($data['password']);
            $tbl_check      = new Manager_Model_checkuser();
            $result         = $tbl_check->check($user, $pass);
		   
            $id             = $result['0']['id'];
            if ($id>0)
            {
                    $ns = new Zend_Session_Namespace('login');        
			        $ns->user =$user;
			        $ns->id   =	$id;    
            }
            else 
            {
             $ns = new Zend_Session_Namespace('lang');
	         $lang=$ns->lang;
             if ($lang==1)	
	             {
	        echo '<script>alert("Tên đăng nhập hoặc mật khẩu không đúng!");</script>';
	             	 
	             } 
             else 
                 {
                 	echo '<script>alert("Username and password do not match!");</script>';
                 } 
            }
		}  
  	  
     }
     
    public function languageAction(){
      $id           = $this->_arrParam['id'];
      $ns = new Zend_Session_Namespace('lang');
      $ns->lang=$id;
      $this->_helper->viewRenderer->setNoRender();
      $link=JURI;
      header("Location: $link");	
     }
   
    public function getcatAction(){
  	   
   	    $tbl_category = new Default_Model_index();
   	    $id           = $this->_arrParam['id'];
   	    $data         = $tbl_category->get_cat($id);
   	    $this->view->data=$data;
		$this->_helper->layout->disableLayout();
     }
     
    public function getproductAction(){
  	   
   	    $tbl_category = new Default_Model_index();
   	    $id           = $this->_arrParam['id'];
   	    $data         = $tbl_category->get_product($id);
   	    $this->view->data=$data;
		$this->_helper->layout->disableLayout();
     }
     
    public function getsolutionAction(){
  	   
   	    $tbl_category = new Default_Model_index();
   	    $id           = $this->_arrParam['id'];
   	    $data         = $tbl_category->get_solution($id);
   	    $this->view->data=$data;
		$this->_helper->layout->disableLayout();
     }
     
    public function langAction(){
  	   
   	    $tbl_category = new Default_Model_index();
   	    $id           = $this->_arrParam['id'];
   	    $data         = $tbl_category->get_cat($id);
   	    $this->view->data=$data;
		$this->_helper->layout->disableLayout();
     }

    public function aboutAction(){
  	  $id           = $this->_arrParam['id'];
  	  $tbl_about    = new Default_Model_about();
  	  $data         = $tbl_about->get_about($id);
  	  $this->view->data=$data;
   	   
     }

    public  function solutionAction(){
      $id                  = $this->_arrParam['id'];
      $cat                 = $this->_arrParam['catid'];
      $tbl_left            = new Default_Model_left();
      
      $this->view->details = $tbl_left->get_detail_solution($id);
      $this->view->title   = $tbl_left->get_title_solution($cat);
      $this->view->product = $tbl_left->get_product_relate_solution($id);      
    
    }
    
    public  function detailsAction(){
      $id                    = $this->_arrParam['id'];
      
      $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
      
      $tbl_product           = new Default_Model_product();
      $this->view->product   = $tbl_product->get_product_details($id);
      $this->view->commnets  = $tbl_product->get_commnets($id);
      
      if($this->_request->isPost()==true){
         $data=$this->_arrParam;
         $error=0;
         $ns = new Zend_Session_Namespace('lang');
	     $lang=$ns->lang;
	    
         if ($data['name1']=='')
		   {
		   	  $error=1;
		   	  if ($lang==1) $this->view->name  ='Nhập họ tên của bạn ';
		   	  else  $this->view->name  ='Enter name ';
		   }
		 elseif ($data['tieude']=='')
		     {
		      $error=1;
		      if ($lang==1) $this->view->tieude  ='Nhập tiêu đề ';
		   	  else  $this->view->tieude  ='Enter name ';
		     }
		 elseif ($data['email']=='')
		     {
		      $error=1;
		      if ($lang==1) $this->view->email  ='Nhập địa chỉ email';
		   	  else  $this->view->email  ='Enter email ';
		     }
		 elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $data['email'])) 
		     {
		       $error=1;
		      if ($lang==1) $this->view->email  ='Địa chỉ email không đúng';
		   	  else  $this->view->email  ='Enter email ';
		     }
         elseif ($data['address']=='')
		     {
		      $error=1;
		      if ($lang==1) $this->view->address  ='Nhập địa chỉ của bạn';
		   	  else  $this->view->address  ='Enter email ';
		     }
        elseif ($data['fulltext']=='')
		     {
		      $error=1;
		     if ($lang==1) $this->view->fulltext  ='Nhập nội dung ý kiến';
		   	  else  $this->view->fulltext  ='Enter email ';
		     }
		     
		 if ($error==0)
		    {
		     $tbl_product->save_commnents($data);
		     $link=JURI;
		     echo '<script>alert("ý kiến của bạn đã đc gửi");document.location.href="'.$link.'";
		      	     </script>';
		    	
		    }
      }
  }

    public  function categoryAction(){
       $id           = $this->_arrParam['id'];
       $tbl_left     = new Default_Model_left();
       $tbl_category = new Default_Model_category();
       
       $this->view->solu_data     = $tbl_left->get_title_solution($id);
      
       $currentPage = 1;
       $i = $this->_getParam('page',1);
       if(!empty($i))
        {
       	    $currentPage = $i;
       	}
        $paginator = Zend_Paginator::factory($tbl_category->get_list_solution($id));
        $paginator->setItemCountPerPage(30)
                  ->setPageRange(10)
                  ->setCurrentPageNumber($currentPage);
        $this->view->paginator = $paginator;
    
    }

    public  function productAction(){
       $id           = $this->_arrParam['id'];
      
       $tbl_category = new Default_Model_category();
       $this->view->cat_data     = $tbl_category->get_title_product($id);
      
       $currentPage = 1;
       $i = $this->_getParam('page',1);
       if(!empty($i))
        {
       	    $currentPage = $i;
       	}
        $paginator = Zend_Paginator::factory($tbl_category->get_list_product($id));
        $paginator->setItemCountPerPage(30)
                  ->setPageRange(10)
                  ->setCurrentPageNumber($currentPage);
        $this->view->paginator = $paginator;
    }
    
    public  function loginAction(){
        if($this->_request->isPost()==true)
		{	
			$data           = $this->_arrParam;
			$user           = $data['user'];
			$pass           = md5($data['password']);
            $tbl_check      = new Manager_Model_checkuser();
            $result         = $tbl_check->check($user, $pass);
		   
            $id             = $result['0']['id'];
            if ($id>0)
            {
                    $ns = new Zend_Session_Namespace('login');        
			        $ns->user =$user;
			        $ns->id   =	$id;    
            }
            else 
            {
             $ns = new Zend_Session_Namespace('lang');
	         $lang=$ns->lang;
             if ($lang==1)	
	             {
	        echo '<script>alert("Viet nam");document.location.href="'.JURI.'"</script>';
	             	 
	             } 
             else 
                 {
                 	echo '<script>alert("Username and password do not match");document.location.href="'.JURI.'"</script>';
                 } 
            }
		}  
      $this->_helper->viewRenderer->setNoRender();
      $this->_helper->layout->disableLayout();
    }

    public function exitAction()
 	{
	  $ns = new Zend_Session_Namespace('login');         
      $ns->unsetAll();
	  echo '<script>document.location.href="'.JURI.'"</script>';
	  $this->_helper->viewRenderer->setNoRender();
	  $this->_helper->layout->disableLayout();
	}
	
    public function registerAction()
 	{
	
	 
	}

    public function forgetAction()
 	{
	
	 
	}
}